//
//  MXSStatusPicView.swift
//  weibo
//
//  Created by 孙飞 on 15/12/14.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import SnapKit

private let MXSStatusPicViewItemId = "MXSStatusPicViewItemId"
//item间距
private let itemMargin: CGFloat = 5
//item宽高
private let itemWH: CGFloat = CGFloat(Int(SCREENW - itemMargin * 2 - MXSCellMargin * 2) / 3)


class MXSStatusPicView: UICollectionView {
    
    var sizeConstraint: Constraint?
    
    // 当前配图视图显示的图片数据
    var pic_urls: [MXSStatusPicInfo]? {
        didSet{
            //
//            countLabel.text = "\(pic_urls?.count ?? 0)"
            
            //根据图片张图,计算当前控件的大小
            let size = calcSize(pic_urls?.count ?? 0)
            
            //设置当前控件的大小
            //self.sizeConstraint?.uninstall()
            self.snp_updateConstraints { (make) -> Void in
                self.sizeConstraint = make.size.equalTo(size).constraint
            }
            
            reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
        //********** collectionView  **********//
        
        // 注册cell
        registerClass(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: MXSStatusPicViewItemId)
        
        //设置数据源
        dataSource = self
        
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //大小/间距
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
//        addSubview(countLabel)
//        
//        countLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
    }
    
    // MARK: - 计算当前控件的大小
    private func calcSize(count: Int) -> CGSize {
        
        
        // 计算当前显示的行数与列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        
        
        // 如果是2张 (2 - 1)/3 + 1
        // 如果是5张 (5 - 1)/3 + 1
        
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        
        
        
        // 宽度 = 列数*每一个条目的宽度 + (列数减1)*条目之前的间距
        
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        
//        printLog(itemWH)
        return CGSize(width: width, height: height)
        
    }
    
    
    // 懒加载控件
    
//    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.blackColor(), fontSize: 40)
    
}

// MARK: - UICollectionViewDataSource
extension MXSStatusPicView: UICollectionViewDataSource {
    
    // 返回有多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    
    // 返回cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(MXSStatusPicViewItemId, forIndexPath: indexPath) as! HMStatusPictureViewCell
        cell.pictureInfo = pic_urls![indexPath.row]
        return cell
    }
}

//自定义 cell
class HMStatusPictureViewCell: UICollectionViewCell {
    
    var pictureInfo: MXSStatusPicInfo? {
        didSet{
            
            if let ulrString = pictureInfo?.thumbnail_pic {
                //gif图片的标识
                gifImageVIew.hidden = !ulrString.hasSuffix(".gif")
                
                imgView.sd_setImageWithURL(NSURL(string: ulrString), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        backgroundColor = UIColor.orangeColor()
        //图片 和图标
        contentView.addSubview(imgView)
        contentView.addSubview(gifImageVIew)
        
        
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageVIew.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imgView)
            make.bottom.equalTo(imgView)
        }
    }
    
    // MARK: - 懒加载
    private lazy var imgView: UIImageView = {
        
        let imageView = UIImageView()
        
        //显示 模型 UIViewContentMode
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    // gif 图标
    private lazy var gifImageVIew: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
}

